package net.sxnic.normal.listener;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import net.sxnic.comm.CommConstant;
import net.sxnic.comm.basecode.BaseCodeManager;
import net.sxnic.comm.property.PropertyManager;
import net.sxnic.comm.utils.PropertyUtil;
import net.sxnic.item.ItemConstants;
import net.sxnic.item.zzdw.ZzdwManager;
import net.sxnic.jhitem.itemoperation.ItemOperationManager;
import net.sxnic.ugr.UgrConstant;
import net.sxnic.ugr.dept.DeptManager;
import net.sxnic.ugr.menu.MenuManager;
import net.sxnic.ugr.user.UserManager;
import net.sxnic.util.PropertiesUtils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 
 * Comments: 系统启动类
 * <p>
 * Author：孙宇飞
 * <p>
 * Create Date：2009-11-2
 * <p>
 * 
 * 
 */
public class AppListener implements ServletContextListener {

	private static Log log = LogFactory.getLog(AppListener.class);

	public void contextDestroyed(ServletContextEvent event) {

	}

	public void contextInitialized(ServletContextEvent event) {
		// 基本码表map初始化
		BaseCodeManager basecodeManager = (BaseCodeManager) WebApplicationContextUtils
				.getRequiredWebApplicationContext(event.getServletContext())
				.getBean("basecodeManager");

		CommConstant.BASECODE_MAP = basecodeManager.init();
		log.debug("-----basecode has inited...."
				+ CommConstant.BASECODE_MAP.size());

		// 基本码表map初始化
		MenuManager menuManager = (MenuManager) WebApplicationContextUtils
				.getRequiredWebApplicationContext(event.getServletContext())
				.getBean("menuManager");

		CommConstant.MENU_MAP = menuManager.findMap1();
		log.debug("-----map has inited...." + CommConstant.MENU_MAP.size());

		// 属性表map初始化
		PropertyManager propertyManager = (PropertyManager) WebApplicationContextUtils
				.getRequiredWebApplicationContext(event.getServletContext())
				.getBean("propertyManager");

		CommConstant.PROPERTY_MAP = propertyManager.init();

		log.debug("-----property map has inited..."
				+ CommConstant.PROPERTY_MAP.size());

		// 部门Map初始化
		DeptManager deptManager = (DeptManager) WebApplicationContextUtils
				.getRequiredWebApplicationContext(event.getServletContext())
				.getBean("deptManager");

		// 所有部门
		UgrConstant.DEPT_MAP = deptManager.findMap();
		log.debug("-----dept map has inited...." + UgrConstant.DEPT_MAP.size());

		// 用户Map初始化
		UserManager userManager = (UserManager) WebApplicationContextUtils
				.getRequiredWebApplicationContext(event.getServletContext())
				.getBean("userManager");
		//所有主管部门
		//UgrConstant.USER_ZGBM_MAP = userManager.findMap(DwType.ZGBM);
		log.debug("-----user zgbm map has inited...." + UgrConstant.USER_ZGBM_MAP.size());
		//所有依托单位
		//UgrConstant.USER_YTDW_MAP = userManager.findMap(DwType.YTDW);
		log.debug("-----user ytdw map has inited...." + UgrConstant.USER_YTDW_MAP.size());
		//所有工程技术中心
		//UgrConstant.USER_GCZX_MAP = userManager.findMap(DwType.GCJSZX);
		log.debug("-----user gcjszx map has inited...." + UgrConstant.USER_GCZX_MAP.size());

		UgrConstant.USER_ORDER_MAP = userManager.findUserOrderMap();
		log.debug("-----user order map has inited...."
				+ UgrConstant.USER_ORDER_MAP.size());
		
		//属性文件读取
		PropertiesUtils.read();

		//申报系统专用静态变量
		ItemConstants.SYSTEM_CALALOG_ALIAS = "linfenitem";
		
		//年份处理-默认读取当前年
		String currYear = new SimpleDateFormat("yyyy").format(new Date());
		//大类申报书与当前年的关系 -1=表示前一年 0=当前年 1表示后一年
		String optYearSet = PropertyUtil.getProperty("item.optYear");
		if(StringUtils.isBlank(optYearSet)) optYearSet = "0";//默认当前年=0
		currYear = Integer.toString(Integer.parseInt(currYear) + Integer.parseInt(optYearSet));
		ItemConstants.APP_YEAR=currYear;
		log.debug("---system appYear has setting----"+ItemConstants.APP_YEAR);
		ZzdwManager zzdwManager = (ZzdwManager) WebApplicationContextUtils
				.getRequiredWebApplicationContext(event.getServletContext())
				.getBean("zzdwManager");
		zzdwManager.initAllVerifyDetpMap();
		ItemConstants.ZZDW_MAP.put(ItemConstants.SYSTEM_CALALOG_ALIAS, zzdwManager.findZzdwCodeNameMap());
		ItemOperationManager itemOperationManager = (ItemOperationManager) WebApplicationContextUtils
				.getRequiredWebApplicationContext(event.getServletContext())
				.getBean("itemOperationManager");
		ItemConstants.OPERATION_MAP = itemOperationManager.findOperationMap();
		log.debug("-----operation map has inited...."
				+ ItemConstants.OPERATION_MAP.size());
		ItemConstants.OPERATION_SHORTNAME_MAP = itemOperationManager.findOperationShortnameMap();
		log.debug("-----operation shortname map has inited...."
				+ ItemConstants.OPERATION_SHORTNAME_MAP.size());


		try {
			// 此为必须步骤
			CommConstant.SYSTEM_CONTEXTPATH = event.getServletContext()
					.getContextPath();

			CommConstant.SERVER_IP = InetAddress.getLocalHost()
					.getHostAddress();
			CommConstant.SERVER_INFO = event.getServletContext()
					.getServerInfo().toLowerCase();

		} catch (UnknownHostException e) {
			e.printStackTrace();
		}

	}

}
